﻿/** AulacSoft **/
/** Author: Thangnc **/

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Xml;

using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using Aulac.Rsc.Bss;
using Aulac.Client.Data.XmlQuery;
using Aulac.Rsc.Bss.Tags;
using Aulac.Client.Global.Interface;
using System.Collections.Generic;

namespace Aulac.Client.Controls.Forms.Bss
{
    public partial class FormBssCmdData : Aulac.Client.Controls.Forms.FormCmdDataBaseBtnCtrl
	{
        public FormBssCmdData()
        {
            InitializeComponent();
        }
        public FormBssCmdData(IFormListData pFrmList)
            : base(pFrmList)
        {
            InitializeComponent();

        }
        
		private string mVchDate = "";
        
        //private string mVerbString = "";
        
        #region Properties
        private Dm_TbName mDmTypesName = Dm_TbName.None;
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(false)]
        public Dm_TbName DmTypesName
        {
            /** AulacSoft **/
            /** Author: Rin**/

            get
            { return mDmTypesName; }
            set
            { mDmTypesName = value; }
        }

        Aulac.Rsc.Bss.VchTypes mVchTypeID = Aulac.Rsc.Bss.VchTypes.None;
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(false)]
        public Aulac.Rsc.Bss.VchTypes VchTypeID
		{
            /** AulacSoft **/
            /** Author: Thangnc **/

			get{return mVchTypeID;}
			set{mVchTypeID = value;}
		}

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(false)]
        public string VchDate
		{
            /** AulacSoft **/
            /** Author: Thangnc **/

			get{return mVchDate;}
			set{mVchDate = value; }
		}
        
       /* [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(false)]
        public bool IsUpdateServer
        {
            set { mIsUpdateServer = value; }
            get { return mIsUpdateServer; }
        }*/
        #endregion
        #region override Methods


        #endregion
        #region Private Methods
 
        #endregion
        #region PublicMethod
        /// <summary>
        /// Lấy tên danh mục.
        /// </summary>
        /// <returns>Tên danh mục</returns>
        public string GetTileDmTypesName()
        {
            /** AulacSoft **/
            /** Author: Rin **/

            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("SELECT {0} ", tagItems.DmTypesName.DmName);
            strQuery += string.Format(" FROM {0} ", TableNames.Dm_DmTypesName);
            strQuery += string.Format(" WHERE {0} = @{0}", tagItems.DmTypesName.DmID);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagItems.DmTypesName.DmID, tagItems.DmTypesName.DmID, DmTypesName);

            try
            {
                return XmlProcess.GetValueVchHeader(query.ExecureQueryData(), tagItems.DmTypesName.DmName);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Lấy tên Phiếu.
        /// </summary>
        /// <returns>Tên Phiếu</returns>
        public string GetTileVchTypesName(VchTypes pVchType)
        {
            /** AulacSoft **/
            /** Author: Rin **/

            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("SELECT {0} ", tagVch.Common.VchName);
            strQuery += string.Format(" FROM {0} ", TableNames.Dm_VchTypes);
            strQuery += string.Format(" WHERE {0} = @{0}", tagVch.Common.VchTypeID);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagVch.Common.VchTypeID, tagVch.Common.VchTypeID,(int)(ValueHelper.ChangeType(pVchType, typeof(int))));

            try
            {
                return XmlProcess.GetValueVchHeader(query.ExecureQueryData(), tagVch.Common.VchName);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        /// <summary>
        /// Lấy tên Phiếu.
        /// </summary>
        /// <returns>Tên Phiếu</returns>
        public string GetTileVchTypesName()
        {
            /** AulacSoft **/
            /** Author: Rin **/

            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("SELECT {0} ", tagVch.Common.VchName);
            strQuery += string.Format(" FROM {0} ", TableNames.Dm_VchTypes);
            strQuery += string.Format(" WHERE {0} = @{0}", tagVch.Common.VchTypeID);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagVch.Common.VchTypeID, tagVch.Common.VchTypeID, (int)(ValueHelper.ChangeType(VchTypeID, typeof(int))));

            try
            {
                return XmlProcess.GetValueVchHeader(query.ExecureQueryData(), tagVch.Common.VchName);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        #endregion
        /// <summary>
        /// Cập nhật một Items cho danh mục.
        /// </summary>
        /// <param name="mTableName">Tên bảng cần Update.</param>
        /// <param name="arrTagFieldUpdate">Danh sách các trường cần cập nhật</param>
        /// <param name="mListControlsValue">List các Control chứa giá trị các trường cần Update</param>
        /// <param name="mTagFieldInfo">Tên trường điều kiện</param>
        /// <param name="mValueInfo">Giá trị của điều kiện</param>
        public void UpdateItem(string mTableName, string[] arrTagFieldUpdate, List<IAulacDataControl> mListControlsValue, string mTagFieldInfo, string mValueInfo)
        {
            /** AulacSoft **/
            /** Author: Rin **/

            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("UPDATE {0} SET ", mTableName);
            strQuery += FormatStringFieldUpdate(arrTagFieldUpdate, mListControlsValue);
            strQuery += string.Format(" WHERE {0} = @{0} ", mTagFieldInfo);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(mTagFieldInfo, mTagFieldInfo, mValueInfo);

            try
            {
                query.ExecureNoneQuery();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public void UpdateItem(string mTableName, string[] arrTagFieldUpdate, object[] mArrValue, string mTagFieldInfo, string mValueInfo)
        {
            /** AulacSoft **/
            /** Author: Rin **/

            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("UPDATE {0} SET ", mTableName);
            strQuery += FormatStringFieldUpdate(arrTagFieldUpdate, mArrValue);
            strQuery += string.Format(" WHERE {0} = @{0} ", mTagFieldInfo);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(mTagFieldInfo, mTagFieldInfo, mValueInfo);

            try
            {
                query.ExecureNoneQuery();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Add dữ liệu trường cần Update.
        /// </summary>
        /// <param name="arrTagFieldUpdate">Những trường cần Update.</param>
        /// <param name="mListControlsValue">Danh sách các Giá trị của trường cần Update(List các Controls).</param>
        /// <returns></returns>
        public string FormatStringFieldUpdate(string[] arrTagFieldUpdate, List<IAulacDataControl> mListControlsValue)
        {
            /** AulacSoft **/
            /** Author: Rin **/

            string ret = "";
            for (int i = 0; i < arrTagFieldUpdate.Length; i++)
            {
                string sValue = AddValueParamFieldUpdate(arrTagFieldUpdate[i], mListControlsValue);
                if (sValue.Trim().Length <= 0) sValue = "NULL";                
                if ((i + 1) == arrTagFieldUpdate.Length)
                    ret += string.Format(" {0} = {1} ", arrTagFieldUpdate[i], sValue);
                else
                    ret += string.Format(" {0} = {1}, ", arrTagFieldUpdate[i], sValue);
            }
            return ret;
        }

        public string FormatStringFieldUpdate(string[] arrTagFieldUpdate, object[] mArrValue)
        {
            /** AulacSoft **/
            /** Author: Rin **/

            string ret = "";
            for (int i = 0; i < arrTagFieldUpdate.Length; i++)
            {
                string s = "";
                if (mArrValue[i].GetType() == typeof(bool))
                {
                    if (mArrValue[i].ToString().ToUpper().Trim().Equals("TRUE"))
                        s = "'1'";
                    else
                        s = "'0'";
                }
                else if (mArrValue[i].GetType() == typeof(string))
                    s = "N'" + mArrValue[i].ToString() + "'";
                else if (mArrValue[i].GetType() == typeof(DateTime))
                    s = "'" + mArrValue[i].ToString() + "'";
                else if ((mArrValue[i].GetType() == typeof(VchTypes)))// || (mArrValue[i].GetType() == typeof(StatusRequests)) || (mArrValue[i].GetType() == typeof(StatusPntPains)) || (mArrValue[i].GetType() == typeof(StatusPatient)))
                {
                    int mvalue = (int)ValueHelper.ChangeType(mArrValue[i], typeof(int));
                    s = "'" + mvalue.ToString() + "'";
                }
                else
                    s = mArrValue[i].ToString();
                if ((i + 1) == arrTagFieldUpdate.Length)
                    ret += string.Format(" {0} = {1} ", arrTagFieldUpdate[i], s);
                else
                    ret += string.Format(" {0} = {1}, ", arrTagFieldUpdate[i], s);
            }
            return ret;
        }

        private string AddValueParamFieldUpdate(string pTagName, List<IAulacDataControl> mListCtrls)
        {
            /** AulacSoft **/
            /** Author: Rin **/

            foreach (IAulacDataControl ctr in mListCtrls)
            {
                object o = ((IAulacValueDataCtrl)ctr).GetValue();
                if (((IAulacDataFieldMemberCtrl)ctr).FieldMember.Equals(pTagName))
                {                    
                    if (o.GetType() == typeof(bool))
                    {
                        if (o.ToString().ToUpper().Trim().Equals("TRUE"))
                            return "'1'";
                        else
                            return "'0'";
                    }
                    else if (o.GetType() == typeof(string))
                        return "N'" + o.ToString() + "'";
                    else if (o.ToString().Trim().Length == 0)
                        return "  ";
                    else
                        return o.ToString();
                }
                else if (ctr is Aulac.Client.Controls.UserControls.ComboTableCtrl)
                {
                    return o.ToString();
                }
            }
            return "";
        }
    
    }
}

